package com.sgu6.sgu6_machine.ui.fragment;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.enums.MachineStatusEnum;
import com.sgu6.middleware.enums.MillActionEnum;
import com.sgu6.middleware.event.MakeRiceEvent;
import com.sgu6.middleware.event.MakeWaterEvent;
import com.sgu6.middleware.manager.ThreadManager;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.netty.utils.NettyHelper;
import com.sgu6.redis.cache.OrderCache;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.event.FinishOrderEvent;
import com.sgu6.sgu6_machine.event.MillStatusEvent;
import com.sgu6.sgu6_machine.event.WeightEvent;
import com.sgu6.sgu6_machine.presenter.MachineP;
import com.sgu6.sgu6_machine.task.MillTimeoutRunnable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class WaterF extends BaseF {

    public static final String TAG = WaterF.class.getSimpleName();

    private Unbinder unbinder;

    @BindView(R.id.tvMillDetail)
    TextView tvMillDetail;
    @BindView(R.id.tvMillNotice)
    TextView tvMillNotice;
    @BindView(R.id.progress)
    ImageView ivProgress;

    private MediaPlayer player;

    public static WaterF newInstance(String params) {
        return new WaterF();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_water, container, false);
        unbinder = ButterKnife.bind(this, v);
        Glide.with(requireContext()).load(R.drawable.milling).into(ivProgress);
        play(R.raw.water);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMakeWaterEvent(MakeWaterEvent event) {
        MachineContainer.orderInfoCacheDto.setMakeNum(event.makeNo);
        MachineContainer.orderInfoCacheDto.setOrderNo(event.orderNo);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void play(int id) {
        ThreadManager.getThreadPollProxy().execute(() -> {
            try {
                if (player != null && player.isPlaying()) {
                    player.pause();
                    player.reset();
                }
                player = MediaPlayer.create(requireContext(), id);
                player.start();//开始或恢复播放
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
